﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Chinaer.Core.Helper;
using System.IO;

namespace Chinaer.Core.Helper
{
    /// <summary>
    /// xmlHelper类
    /// </summary>
    public sealed class OrmXmlHelper
    {
        private XmlDocument doc = null;
        /// <summary>
        /// xml路径
        /// </summary>
        private string xmlFilePath = "Common.xml";
        private string ormFilePath = string.Empty;
        public OrmXmlHelper()
        {
            this.ormFilePath = ConfigHelper.GetAppSetting("ormFilePath") + xmlFilePath;
        }
        public OrmXmlHelper(string _xmlFilePath)
        {
            this.ormFilePath = _xmlFilePath + xmlFilePath;
        }

        private string GetExecutingPath()
        {
            return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        }

        public List<Model.Orm.Model_OrmStatement> GetSqlByXml()
        {
            // xmlFilePath = GetExecutingPath() + @"\" + xmlFilePath;
            if (this.ormFilePath == null || FileHelper.CheckFileExists(this.ormFilePath) == false)
            {
                throw new FileNotFoundException(string.Format("要解析的文件不存在{0}。", this.ormFilePath));
            }
            doc = new XmlDocument();
            Stream stream=new FileStream(ormFilePath,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
            doc.Load(stream);
            stream.Close();
            //准备读取文件
            XmlNode rootConfiguration = doc.SelectSingleNode("configuration");
            if (rootConfiguration == null)
            {
                ExceptonHelper.ThrowException("orm configuration 结点错误");
            }
            XmlNode ormstatementsNode = rootConfiguration.SelectSingleNode("OrmStatements");
            XmlNodeList ormStatementNodeList = ormstatementsNode.SelectNodes("OrmStatement");

            List<Model.Orm.Model_OrmStatement> list_OrmStatement = new List<Model.Orm.Model_OrmStatement>();//sql列表
            if (ormStatementNodeList != null)
            {
                XmlNode statementNameNode = null;
                XmlNode mapClassNode = null;
                XmlNode statementNode = null;

                foreach (XmlNode item in ormStatementNodeList)
                {
                    statementNameNode = item.SelectSingleNode("StatementName");
                    mapClassNode = item.SelectSingleNode("MapClass");
                    statementNode = item.SelectSingleNode("Statement");
                    Model.Orm.Model_OrmStatement modelStatement = new Model.Orm.Model_OrmStatement()
                    {
                        StatementName = statementNameNode == null ? string.Empty : statementNameNode.InnerText,
                        MapClass = mapClassNode == null ? string.Empty : mapClassNode.InnerText,
                        Statement = statementNode == null ? string.Empty : statementNode.InnerText
                    };
                    list_OrmStatement.Add(modelStatement);
                }
            }
            return list_OrmStatement;
        }
    }
}
